// 页面路径：store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import { expressListQuery, expressCompanyListQueryByPosition } from "../controller/request_ctrl"

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		expressAllList: null,
		expressItem: null,
		expressRecord: null,
		expressPriceList: null,
		
		expressAddress: '',
		expressCom: '',
		expressWdList: [],
		expressWdIndex: 1
	},
	mutations: {
		updateExpressList(state, payload) {
			// 变更状态
			state.expressAllList = payload.list;
		},
		updateExpressItem(state, payload) {
			if (!payload) {
				state.expressItem = null;
				return;
			}
			// 变更状态
			state.expressItem = payload.item;
		},
		updateExpressRecord(state, payload) {
			if (!payload || !payload.item) {
				return;
			}
			state.expressRecord = payload.item;
		},
		updateExpressPriceList(state, payload) {
			if (!payload || !payload.item) {
				return;
			}
			state.expressPriceList = payload.item;
		},
		updateExpressWdList(state, payload) {
			if (!payload || !payload.item) {
				return;
			}
			state.expressWdList = payload.item;
		}
	},
	actions: {
		getExpressList(context , payload) {
			if (this.expressAllList) {
				return Promise.resolve();
			}
			return expressListQuery().then((res) => {
				if (res.showapi_res_code === 0) {
					context.commit('updateExpressList',{
						list: res.showapi_res_body.express_list
					});
					console.log('---- expressListQuery ----:', res.showapi_res_body.express_list)
				}
			}).catch((err) => {
				console.log('---- expressListQuery ----:', err)
			})
		},
		setExpressItem(context , payload) {
			context.commit('updateExpressItem', payload);
		},
		setExpressRecord(context , payload) {
			context.commit('updateExpressRecord', payload);
		},
		setExpressPriceList(context , payload) {
			context.commit('updateExpressPriceList', payload);
		},
		setExpressWdList(context, payload) {
			if (payload.expressAddress) {
				this.expressAddress = payload.expressAddress;
			}
			if (payload.expressCom) {
				this.expressCom = payload.expressCom;
			}
			if (this.expressWdIndex) {
				this.expressWdIndex = payload.pageIndex;
			}
			expressCompanyListQueryByPosition(this.expressAddress, this.expressCom, this.expressWdIndex).then((res) => {
				if (res.showapi_res_code === 0) {
					if (res.showapi_res_body.contentlist) {
						context.commit('updateExpressWdList', {
							item: res.showapi_res_body.contentlist
						});
					}
				}
			});
		}
	}
})
export default store